﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProfilesController.cs" company="DissingNellemann">
//   
// All rights reserved </copyright>
// <summary>
//   The profiles controller.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Website.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    using Website.CRUDServices;
    using Website.Models;
    using Website.Models.Styling;
    using Website.Models.ViewModels;

    /// <summary>
    ///     The profiles controller.
    /// </summary>
    public class ProfilesController : BaseController
    {
        // GET: /Profile/
        #region Public Methods and Operators

        /// <summary>
        ///     The create profile.
        /// </summary>
        /// <returns>
        ///     The <see cref="ActionResult" />.
        /// </returns>
        public ActionResult CreateProfile()
        {
            ProfilesView pv = ProfilesView.GetRelated();
            this.ViewData["Regions"] = pv.Regions;
            this.ViewData["CreateLocationActionName"] = "CreateLocationJson";
            return View(pv);
        }

        /// <summary>
        /// The create profile.
        /// </summary>
        /// <param name="profile">
        /// The profile.
        /// </param>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        [HttpPost]
        public ActionResult CreateProfile(Profile profile)
        {
            string errorMsg = this.ValidateProfile(profile);

            if (!errorMsg.Equals(string.Empty))
            {
                ProfilesView pv = ProfilesView.GetRelated();
                this.ViewData["Regions"] = pv.Regions;
                this.ViewData["CreateLocationActionName"] = "CreateLocationJson";
                this.ViewData["ErrorMessage"] = new HtmlString(@"<ul>" + errorMsg + @"</ul>");
                return View(pv);
            }

            CRUDServiceProvider.WithCRUDService(
                client =>
                    {
                        var cmd = new CreateProfileCommand();
                        profile.ID = Guid.NewGuid();
                        cmd.Profile = profile;

                        client.ExecuteCommand(cmd);
                    });

            return this.RedirectToAction("Index");
        }

        /// <summary>
        /// The index.
        /// </summary>
        /// <param name="skip">
        /// The skip.
        /// </param>
        /// <param name="take">
        /// The take.
        /// </param>
        /// <param name="ids">
        /// The ids.
        /// </param>
        /// <param name="orderBy">
        /// The order by.
        /// </param>
        /// <param name="sortAscending">
        /// The sort ascending.
        /// </param>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Index(int? skip, int? take, List<Guid> ids, string orderBy, bool? sortAscending)
        {
            this.Init("Profiles");

            ProfilesView pv = ProfilesView.GetRelated();

            var headerItems = new List<TableViewHeaderItem>();
            var item1 = new TableViewHeaderItem { Text = "Navn", SortableName = "Name", IsSortable = true, };

            var item2 = new TableViewHeaderItem { Text = "Relation", SortableName = "Relation", IsSortable = true };
            var item3 = new TableViewHeaderItem { Text = "Sektor", SortableName = "Sector", IsSortable = true };
            var item4 = new TableViewHeaderItem { Text = "Type", SortableName = "Type", IsSortable = true };
            headerItems.Add(item1);
            headerItems.Add(item2);
            headerItems.Add(item3);
            headerItems.Add(item4);

            pv.SortAscending = sortAscending == null || (bool)sortAscending;
            pv.TableHeader = TableViewHeader.GetTableHeader("Index", 20, orderBy, headerItems, pv.SortAscending);
            pv.Skip = skip != null ? (int)skip : 0;
            pv.Take = take != null ? (int)take : 20;

            pv.Profiles = ProfilesView.GetProfilesList(orderBy, ids, pv);

            return View(pv);
        }

        /// <summary>
        /// The update profile.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult UpdateProfile(Guid id)
        {
            ProfilesView pv = ProfilesView.GetRelated();
            this.ViewData["Regions"] = pv.Regions;
            this.ViewData["CreateLocationActionName"] = "CreateLocationJson";

            Profile profile = null;

            CRUDServiceProvider.WithCRUDService(
                client =>
                    {
                        var q = new ProfilesQuery { Settings = new SearchSettings { IDEquals = id } };

                        var rs = (ProfilesResult)client.Query(q);

                        if (rs != null && rs.List.Any())
                        {
                            profile = rs.List[0];
                        }
                    });

            this.ViewData["Profile"] = profile;

            return View(pv);
        }

        /// <summary>
        /// The update profile.
        /// </summary>
        /// <param name="profile">
        /// The profile.
        /// </param>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        [HttpPost]
        public ActionResult UpdateProfile(Profile profile)
        {
            string errorMsg = this.ValidateProfile(profile);

            if (!errorMsg.Equals(string.Empty))
            {
                ProfilesView pv = ProfilesView.GetRelated();
                this.ViewData["Regions"] = pv.Regions;
                this.ViewData["CreateLocationActionName"] = "CreateLocationJson";
                this.ViewData["ErrorMessage"] = new HtmlString(@"<ul>" + errorMsg + @"</ul>");
                return View(pv);
            }

            CRUDServiceProvider.WithCRUDService(
                client =>
                    {
                        var cmd = new UpdateProfileCommand { UpdatedProfile = profile };
                        client.ExecuteCommand(cmd);
                    });

            return this.RedirectToAction("Index");
        }

        #endregion

        #region Methods

        /// <summary>
        /// The validate profile.
        /// </summary>
        /// <param name="profile">
        /// The profile.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string ValidateProfile(Profile profile)
        {
            string errorMsg = string.Empty;
            if (profile.ContactType.Equals(Guid.Empty))
            {
                errorMsg += @"<li>Der skal vælges en kontakt type.</li>";
            }

            if (profile.Location.Equals(Guid.Empty))
            {
                errorMsg += @"<li>Der skal vælges en lokation.</li>";
            }

            if (profile.Relation.Equals(Guid.Empty))
            {
                errorMsg += @"<li>Der skal vælge en type relation.</li>";
            }

            if (profile.SubType.Equals(Guid.Empty))
            {
                errorMsg += @"<li>Der skal vælges en profile-type.</li>";
            }

            return errorMsg;
        }

        #endregion
    }
}